﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.IO;


namespace MBSPL.Utility
{
    public static class Utility
    {
        public static Dictionary<string, string> getSeatAvailability(string xmlString)
        {
            Dictionary<string, string> dctSeatAvailability = new Dictionary<string, string>();
            //string xmlString = "<seatlayout><seat status='Available'>A1</seat><seat status='Available'>A2</seat><seat status='Available'>A3</seat><seat status='Available'>A4</seat><seat status='Available'>A5</seat><seat status='Available'>A6</seat><seat status='Available'>A7</seat><seat status='Available'>A8</seat></seatlayout>";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(new StringReader(xmlString));
            XmlNodeList nodeList= xmlDoc.SelectNodes("/seatlayout/seat");
            
            foreach (XmlNode node in nodeList)
            {
                string seatName = node.InnerText;
                string status = node.Attributes["status"].Value;
                Console.WriteLine("Seat Name is: " + seatName);
                Console.WriteLine("Seat Status is: " + status);
                dctSeatAvailability.Add(seatName,status);
            }
            // Below code was giving compilation error..have changed it..
          //var seatInfo=from node in xmlDoc.Descendants("seatlayout")
          //            select new {
          //            status=node.Attribute("status").Value,
          //            seatno=node.Value
          //            };
         
            //foreach (var seat in seatInfo)
            //{
            //    dctSeatAvailability.Add(seat.seatno, seat.status);
            //}
            return dctSeatAvailability;
        }

        public static string setseatStatusBooked(string xmlString,string seatno)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(new StringReader(xmlString));
            XmlNode node = xmlDoc.SelectSingleNode("seatlayout/seat/" + seatno);
            node.Attributes["status"].Value = "Booked";
            xmlDoc.Save(xmlString);

            return xmlString;
        
        }
    }
}